package com.wwinsoft.framework.service.security;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wwinsoft.framework.dao.security.AuthorityDao;
import com.wwinsoft.framework.dao.security.ResourceDao;
import com.wwinsoft.framework.entity.security.Authority;
import com.wwinsoft.framework.entity.security.Resource;
import com.wwinsoft.framework.pojo.common.JsNode;

/**
 * @author: Tim Chen
 * @since 1.0
 */
@Component
@Transactional
public class AuthorityService {

    @Autowired
    private ResourceDao resourceDao;

    @Autowired
    private AuthorityDao authorityDao;

    public Authority getAuthority(Long id) {
        return authorityDao.get(id);
    }

    public void saveAuthority(Authority authority) {
        authorityDao.save(authority);
    }

    public void deleteAuthority(Long id) {
        authorityDao.delete(id);
    }

    public Resource getResource(Long id) {
        return resourceDao.get(id);
    }

    public void saveResource(Resource resource) {
        resourceDao.save(resource);
    }

    public void deleteResource(Long id) {
        resourceDao.delete(id);
    }


    public Long updateNode(JsNode<Long> jsNode) throws Exception {
        if ("remove_node".equalsIgnoreCase(jsNode.getOperation())) {
            authorityDao.deleteAllChildrenBySQL(jsNode.getId());
            return jsNode.getId();
        }

        if (("move_node").equalsIgnoreCase(jsNode.getOperation()) && jsNode.isCopy()) {
            Authority oldAuthority = authorityDao.get(jsNode.getId());
            jsNode.setName(oldAuthority.getName());
            jsNode.setType(oldAuthority.getNodeType());
            jsNode.setId(null);
        }

        Authority authority;
        if (jsNode.getId() != null) {
            authority = authorityDao.get(jsNode.getId());
        } else {
            authority = new Authority();
        }
        if (StringUtils.isNotEmpty(jsNode.getName())) {
            authority.setName(jsNode.getName());
        }
        if (jsNode.getRevision() != null) {
            authority.setRevision(jsNode.getRevision());
        }
        if (jsNode.getPosition() != null) {
            authority.setPosition(jsNode.getPosition());
        }
        if (jsNode.getType() != null) {
            authority.setResourceType(jsNode.getType());
        }
        if (jsNode.getParentId() != null) {
            authority.setParentId(jsNode.getParentId());
        }
        authorityDao.save(authority);
        return authority.getId();
    }


    @Transactional(readOnly = true)
    public List<Resource> getAllResouce() {
        List<Resource> allResouce = resourceDao.getAll("position", true);
        return allResouce;
    }

    @Transactional(readOnly = true)
    public List<Authority> getAllAuthoritys() {
        List<Authority> authorityList = authorityDao.getAll("position", true);
        return authorityList;
    }
}
